package com.service;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.dao.IXlayoutkDao;
import com.icim.service.ICIMBaseService;
import com.model.Bis_layot;
import com.model.Bis_layot_lc;
import com.model.Bis_mdl_def;
import com.model.Ret_prd_info;
import com.tx.Xlayoutk.XlayoutkI;
import com.tx.Xlayoutk.XlayoutkO;
import com.tx.Xlayoutk.XlayoutkOA;
import com.util.MqOpe;

@Scope("prototype")
@Service("XlayoutkService")
public class XlayoutkService extends ICIMBaseService{
	private Logger logger = Logger.getLogger(XlayoutkService.class);
	@Autowired
	private IXlayoutkDao xlayoutkDao;
	
	public String subMainProc(String strInMsg){
		MqOpe mqOpe1 = new MqOpe();
		XlayoutkI xlayoutkI = new XlayoutkI();
		XlayoutkO xlayoutkO  = new XlayoutkO();
		String reply ="";
		try{
			xlayoutkI = (XlayoutkI) mqOpe1.inStr2Obj(strInMsg,xlayoutkI);
			logger.info("InTrx is "+ strInMsg );
			
			String actionFlg =xlayoutkI.getAction_flg();
			if(actionFlg.equals("I")){
				xlayoutkO = getLayoutByProdSeq( xlayoutkI.getPrd_seq_id() );
			}
		}catch(Exception ex){
			xlayoutkO.setTrx_id("XLAYOUTK");
			xlayoutkO.setTrx_id("O");
			xlayoutkO.setRtn_code("0000002");
			xlayoutkO.setRtn_mesg(ex.toString());
		}

		reply = mqOpe1.obj2Str(xlayoutkO);
		logger.info(reply);
		return reply;
	}
	
	public XlayoutkO getLayoutByProdSeq( String prd_seq_id ){
		MqOpe mqOpe = new MqOpe();
		XlayoutkO xlayoutkO = new XlayoutkO();
		
		String sql = " FROM Ret_prd_info WHERE prd_seq_id= '"+ prd_seq_id + "'";
		logger.info("Query sql:" + sql );
		List<Ret_prd_info> prodInfoList = xlayoutkDao.find(sql);
		
		if( prodInfoList.size() == 0 ){
			xlayoutkO.setTrx_id("XLAYOUTK");
			xlayoutkO.setTrx_id("O");
//			xlayoutkO.setRtn_code(comL2S(this.getE_RET_PROD_INFO() + this.getE_READ_NOT_FOUND()));
			xlayoutkO.setRtn_mesg("not found record in Ret_prd_info where prd_seq_id='"+prd_seq_id+"'");
		    logger.info( mqOpe.obj2Str(xlayoutkO));
		    return xlayoutkO ;
		}
		
		Ret_prd_info prdInfo = prodInfoList.get(0);
		String mdl_id = prdInfo.getMdl_id_fk();
		List<Bis_mdl_def> bisMdlDefList = new ArrayList<Bis_mdl_def>();
		String sqlMdl = "FROM Bis_mdl_def WHERE mdl_id = '"+mdl_id+"'";
		bisMdlDefList = xlayoutkDao.find( sqlMdl );
		
		if( bisMdlDefList.size()==0 ){
			xlayoutkO.setTrx_id("XLAYOUTK");
			xlayoutkO.setTrx_id("O");
//			xlayoutkO.setRtn_code( comL2S(this.getE_BIS_MDL_DEF() + this.getE_READ_NOT_FOUND() ));
			xlayoutkO.setRtn_mesg("not found record in Bis_mdl_def where mdl_id='"+  mdl_id + "'");
		    logger.info( mqOpe.obj2Str(xlayoutkO));
		    return xlayoutkO;
		}
		
		Bis_mdl_def bisMdlDef = bisMdlDefList.get(0);
		String layoutID = bisMdlDef.getLayot_id_fk();
		String layotSql = "FROM Bis_layot WHERE layot_id= '"+ layoutID + "'";
		List<Bis_layot> bisLayotList = new ArrayList<Bis_layot>();
		bisLayotList=xlayoutkDao.find( layotSql );
		
		if( bisLayotList.size()==0 ){
			xlayoutkO.setTrx_id("XLAYOUTK");
			xlayoutkO.setType_id("O");
//			xlayoutkO.setRtn_code(comL2S(this.getE_BIS_LAYOT()+this.getE_READ_NOT_FOUND()));
			xlayoutkO.setRtn_mesg("not found record in Bis_layot where layot_id ='"+layoutID+"'");
			return xlayoutkO;
		}
		Bis_layot bisLayot = bisLayotList.get(0);
		
		String layotLcSql ="FROM Bis_layot_lc WHERE layot_id_fk ='" + layoutID +"' ORDER BY MRR_LOC";
		List<Bis_layot_lc> bisLayotLcList  = xlayoutkDao.find(layotLcSql);
		if( bisLayotLcList.size()==0){
			xlayoutkO.setTrx_id("XLAYOUTK");
			xlayoutkO.setType_id("O");
//			xlayoutkO.setRtn_code(comL2S(this.getE_BIS_LAYOT_LC()+this.getE_READ_NOT_FOUND()));
			xlayoutkO.setRtn_mesg("not found record in Bis_layot_lc where layot_id_fk ='"+layoutID+"'");
		}
		int layoutLcCnt = bisLayotLcList.size();
		List<XlayoutkOA> oaryList = new ArrayList<XlayoutkOA>();
		for( Bis_layot_lc layotLc : bisLayotLcList){
			XlayoutkOA oary = new XlayoutkOA();
			oary.setX_axis(layotLc.getId().getX_axis());
			oary.setY_axis(layotLc.getId().getY_axis());
			oary.setPos_id(layotLc.getPos_id());
			oary.setMrr_loc(layotLc.getMrr_loc());
			oaryList.add(oary);
		}
		xlayoutkO.setTrx_id("XPLAYOUTK");
		xlayoutkO.setType_id("O");
		xlayoutkO.setRtn_code("0000000");
		xlayoutkO.setRtn_mesg("success");
		xlayoutkO.setX_axis_cnt(Integer.toString(bisLayot.getX_axis_cnt()));
		xlayoutkO.setY_axis_cnt(Integer.toString(bisLayot.getY_axis_cnt()));
		xlayoutkO.setPos_len(Integer.toString(bisLayot.getPos_len()));
		xlayoutkO.setOary(oaryList);
		xlayoutkO.setTbl_cnt(Integer.toString(oaryList.size()));
		return xlayoutkO ;
	}
	
}
